package com.tianmao.servlet;

import com.tianmao.dao.ProductDao;
import com.tianmao.model.Product;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 该Servlet处理根据产品编号查询产品的请求,然后将查询出来的产品信息交给productInfo.jsp显示
 *
 */
@WebServlet("/ps")
public class ProductServlet extends HttpServlet {

    ProductDao productDao = new ProductDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1. 获取请求参数
        String pid=req.getParameter("pid");
        Product product = productDao.findProductById(pid);

       /*  会话范围太大, 不适合用
        HttpSession session = req.getSession(true);
        session.setAttribute("product",product);*/

        // 将产品信息放入请求对象request. 并转发给 productInfo.jsp  为什么要转发?
        req.setAttribute("product",product);
        // 将当前请求对象和响应对象转发给 productInfo.jsp
        req.getRequestDispatcher("productInfo.jsp").forward(req,resp);

    }
}
